package com.zr.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.dao.DeleteteasDaoljc;
import com.zr.daoImpl.DeleteteasDaoImplljc;
import com.zr.service.DeleteteasServiceljc;
import com.zr.serviceImpl.DeleteteaServiceImplljc;
import com.zr.util.JDBCUtil;

public class DeleteteasControllerljc extends HttpServlet{
	
	// 删除教师控制器
	DeleteteasServiceljc deletes = new DeleteteaServiceImplljc();
	DeleteteasDaoljc deleted = new DeleteteasDaoImplljc();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		boolean flag = true;
		Connection con = JDBCUtil.getConnection();
		String ck[] = req.getParameterValues("tid[]");
		for (int i = 0; i < ck.length; i++) {
			int i1 = Integer.parseInt(ck[i]);
			int uid = deleted.findUidByTid(i1).getUid();
			try {
				con.setAutoCommit(false);
				deleted.deleteTeaByTid(i1, con);
				deleted.deleteUserByUid(uid, con);
				con.commit();	//手动提交
			} catch (SQLException e) {
				try {
					con.rollback();
					flag = false;
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
			if(flag){
				pw.write("1");
			}else{
				pw.write("0");
			}
			pw.flush();
			pw.close();
		}
		
	}
}
